package spring.cloud.tasks.tasks_assign_manager.task_item_assign.pick_up.executor;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import spring.cloud.tasks.common.task_item_allot.OnlineExecutor;
import spring.cloud.tasks.common.task_item_allot.TaskItem;
import spring.cloud.tasks.tasks_assign_manager.service.TaskItemAssignService;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign.PreferExecutorSettings;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign.TaskItemAssignUtils;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign.pick_up.PickUp;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign.pick_up.executor.task.PickUpWhenTaskExecutorOnline;

import java.util.Iterator;
import java.util.List;

/**
 * 恢复executor流量，平衡摘取分片
 */
@Slf4j
@Setter
@Getter
public class PickUpWhenExecutorRecoverTraffic extends PickUp {

    private String executorId;

    public PickUpWhenExecutorRecoverTraffic(TaskItemAssignService taskItemAssignService) {
        super(taskItemAssignService);

    }

    @Override
    protected void log() {
        log.info("Execute the {} with {} recover traffic", this.getClass().getSimpleName(), executorId);
    }

    @Override
    public boolean pickUp(CuratorFramework curatorFramework,//
                          List<String> taskIdList,//
                          List<String> enableTaskIdList,//
                          List<TaskItem> pickUpEdTaskItemList,//
                          List<TaskItem> discardTaskItemList,//
                          List<OnlineExecutor> onlineExecutorList,//
                          List<OnlineExecutor> ownTrafficOnlineExecutorList,//
                          PreferExecutorSettings preferExecutorSettings
    ) throws Exception {
        // 设置该executor的noTraffic为false
        OnlineExecutor targetOnlineExecutor = null;
        Iterator<OnlineExecutor> iterator = onlineExecutorList.iterator();
        while (iterator.hasNext()) {
            OnlineExecutor onlineExecutor = iterator.next();
            if (onlineExecutor.getExecutorId().equals(executorId)) {
                onlineExecutor.setOwnTraffic(true);
                ownTrafficOnlineExecutorList.add(onlineExecutor);
                targetOnlineExecutor = onlineExecutor;
                break;
            }
        }
        if (targetOnlineExecutor == null) {
            log.warn("The executor {} maybe offline, unnecessary to recover traffic", executorId);
            return false;
        }
        //这段的代码是串行执行
        for (String taskId : targetOnlineExecutor.getSupportedTaskIdList()) {
            boolean hasTaskItemRunning = TaskItemAssignUtils.hasTaskItemRunning(taskId, ownTrafficOnlineExecutorList);
            if (!hasTaskItemRunning) {
                continue;
            }
            //能够走到这一步代表，已经将
            PickUpWhenTaskExecutorOnline pickUpWhenTaskExecutorOnline = new PickUpWhenTaskExecutorOnline(taskItemAssignService);
            pickUpWhenTaskExecutorOnline.setExecutorId(executorId);
            pickUpWhenTaskExecutorOnline.setTaskId(taskId);
            pickUpWhenTaskExecutorOnline.pickUp(curatorFramework, enableTaskIdList, pickUpEdTaskItemList, ownTrafficOnlineExecutorList, preferExecutorSettings);
        }
        return true;
    }


}
